import { ref } from "vue";
import { useEventStore } from "@/store/modules/events";
import { message } from "ant-design-vue";
import { useTipsStore } from "@/store/modules/tips";
const tipsStore = useTipsStore();
const store = useEventStore()
const iframeRef: any = ref();
//导出iframeRef引用，以便在其他组件中使用
export const useIframeRef = () => {
    return iframeRef
};

/**
 * 实验3d交互方向控制
 * @param value 0 前，1左，2右，3后
 */
export const handleChangeDirection = (value: number) => {
    let datas = {
        type: "changeDirection",
        data: {
            value: value,
        },
    };
    iframeRef.value.contentWindow.postMessage(datas, "*");
};
/**
 * 实验3d交互大小控制
 * @param value 数值百分比 如60
 */
export const handleChangeSize = (value: number) => {
    console.log("实验3d交互大小控制", value);
    let datas = {
        type: "changeSize",
        data: {
            value: value,
        },
    };
    iframeRef.value.contentWindow.postMessage(datas, "*");
};

/**
 * 实验3d交互历史记录控制
 * @param value 0后退 1前进
 */
export const handleChangeHistory = (value: number) => {
    console.log("实验3d交互历史记录控制", value);
    let datas = {
        type: "changeHistory",
        data: {
            value: value,
        },
    };
    iframeRef.value.contentWindow.postMessage(datas, "*");
};

/**
 * 实验3d交互分享模式
 * @param value 0后退 1前进
 */
export const handleChangeStep = (value: number) => {
    console.log("实验3d交互前进后退控制", value);
    let datas = {
        type: "changeStep",
        data: {
            value: value,
        },
    };
    iframeRef.value.contentWindow.postMessage(datas, "*");
};
/**
 * 模式切换
 * @param type 模式类型，0为组装模式，1为调试模式
 */
export const useHandleChangeMode = (type: number) => {
    if (iframeRef.value) {
        let datas = {
            type: "changeMode",
            data: type
        }
        iframeRef.value.contentWindow.postMessage(datas, "*");
    }
}
/**
 * 操作控制
 * @param type 方向top,left,right,bottom
 */
export const useHandleChangeOperate = (type: string, isUser: boolean = true) => {

    store.setOperate(type)
    if (iframeRef.value) {
        let datas = {
            type: "changeOperate",
            data: {
                type: type,
                isUser: isUser
            }
        }
        iframeRef.value.contentWindow.postMessage(datas, "*");
    }
}
/**
 * 操作控制
 * @param type 方向top,left,right,bottom
 */
export const useHandleChangeOperate2 = (type: string, isUser: boolean = true) => {
    if (store.chooseType == '') {
        console.log(11, '走这里了')
        message.warn('当前实验未进行到这一步，操作无法执行，请查看实验流程提示。')
        return
    }
    if (store.chooseFinifsh) {
        // message.success('恭喜你，已完成本步操作。')
        return
    }

    store.setOperate2(type)
    if (iframeRef.value) {
        let datas = {
            type: "changeOperate",
            data: {
                type: type,
                isUser: isUser
            }
        }
        iframeRef.value.contentWindow.postMessage(datas, "*");
    }
}
/**
 * 操作控制
 * @param type 方向top,left,right,bottom
 */
export const useHandleChangeOperate3 = (type: string, isUser: boolean = true) => {
    if (store.chooseType == '') {
        console.log(115, '走这里了')
        message.warn('当前实验未进行到这一步，操作无法执行，请查看实验流程提示。')
        return
    }
    if (store.chooseFinifsh) {
        // message.success('恭喜你，已完成本步操作。')
        return
    }
    store.setOperate3(type)
    if (iframeRef.value) {
        let datas = {
            type: "changeOperate",
            data: {
                type: type,
                isUser: isUser
            }
        }
        iframeRef.value.contentWindow.postMessage(datas, "*");
    }
}
/**
 * 操作控制
 * @param type 方向top,left,right,bottom
 */
export const useHandleChangeOperate4 = (type: string, isUser: boolean = true) => {
    if (!store.chooseSure) {
        return
    }
    if (type == "left" || type == "right") {
        store.setOperate4(type)
        if (iframeRef.value) {
            let datas = {
                type: "changeOperate",
                data: {
                    type: type,
                    isUser: isUser
                }
            }
            iframeRef.value.contentWindow.postMessage(datas, "*");
        }
    }
}
/**
 * 控制波长
 * @param number 波长数值，介于0到1000之间nm
 */
export const useHandleWavelength = (number: number) => {
    if (iframeRef.value) {
        let datas = {
            type: "wavelength",
            data: number
        }
        iframeRef.value.contentWindow.postMessage(datas, "*");
    }
}
/**
 * 狭缝宽度
 * @param number 狭缝宽度数值，介于0到1之间mm
 */
export const useHandleSlitwidth = (number: number) => {
    if (iframeRef.value) {
        let datas = {
            type: "slitwidth",
            data: number
        }
        iframeRef.value.contentWindow.postMessage(datas, "*");
    }
}
/**
 * 光栅角度
 * @param number 波光栅角度数值，介于0到360之间°
 */
export const useHandleGratingangle = (number: number) => {
    if (iframeRef.value) {
        let datas = {
            type: "gratingangle",
            data: number
        }
        iframeRef.value.contentWindow.postMessage(datas, "*");
    }
}
/**
 * 开始测量的方法
 */
export const useHandleStartMeasurement = () => {
    //判断operate的x和y是否大于20，小于280，如果不是则不执行以下操作
    if (store.operate.x >= 20 && store.operate.y >= 20 && store.operate.x <= 28 && store.operate.y <= 28) {
        store.setStartMeasurement(1)
        if (iframeRef.value) {
            let datas = {
                type: "startMeasurement",
                data: true
            }
            iframeRef.value.contentWindow.postMessage(datas, "*");
        }
    } else {
        message.error("需要校准后才能测量")
    }

}
/**
 * 重置参数的方法
 */
export const useHandleResetParameters = () => {
    if (store.startMeasurement == 2) {
        store.setResetParameters(true)
        if (iframeRef.value) {
            let datas = {
                type: "resetParameters",
                data: true
            }
            iframeRef.value.contentWindow.postMessage(datas, "*");
        }
    } else {
        message.error("请先开始测量,或等到测量完成后再重置参数")
    }

}
/**
 * 保存配置的方法
 */
export const useHandleSaveConfiguration = () => {
    if (iframeRef.value) {
        let datas = {
            type: "saveConfiguration",
            data: true
        }
        iframeRef.value.contentWindow.postMessage(datas, "*");
    }
}

/**
 * 操作提示的方法

 */
export const useHandleOptionsTip = () => {
    if (iframeRef.value?.contentWindow) {
        const datas = {
            type: "handleOptionsTip",
        };
        iframeRef.value.contentWindow.postMessage(datas, "*");
    }
}
/**
 * 关闭提示框的方法

 */
export const useHandleCloseTip = () => {
    if (iframeRef.value?.contentWindow) {
        const datas = {
            type: "handleCloseTip",
        };
        iframeRef.value.contentWindow.postMessage(datas, "*");
    }
}
/**
 * 提示框设置的方法
 * @param data 
 */
export const useHandleSetStoreTip = (data: any) => {
    tipsStore.setnotAutoTipContent(data.content)
    tipsStore.setStepTip(data.status)
    tipsStore.setShowNotAutoTip(true)
}
/**
 * 操作自动提示的方法
 */
export const useHandleAutoTip = () => {
    if (iframeRef.value?.contentWindow) {
        const datas = {
            type: "handleAutoTip",
        };
        iframeRef.value.contentWindow.postMessage(datas, "*");
    }
}
/**
 * 关闭提示框的方法

 */
export const useHandleCloseAutoTip = () => {
    if (iframeRef.value?.contentWindow) {
        const datas = {
            type: "handleCloseAutoTip",
        };
        iframeRef.value.contentWindow.postMessage(datas, "*");
    }
}
/**
 * 自动提示框设置的方法
 * @param data 
 */
export const useHandleSetStoreAutoTip = (data: any) => {
    tipsStore.setAutoTipContent(data.content)
    tipsStore.setStepTip(data.status)
    tipsStore.setShowAutoTip(true)
    if(data.isFinished==true){
         tipsStore.setIsExitSatus(true);
    }
}

/**
 * 下一步提示框设置的方法
 */
export const useHandleSetNext = () => {
    if (iframeRef.value?.contentWindow) {
        const datas = {
            type: "handleNextTip",
        };
        iframeRef.value.contentWindow.postMessage(datas, "*");
        tipsStore.setIsNext(false)
    }
}


/**
 * 切换实验参数和实验现象 num:1为实验参数，2为实验现象
 */
export const useHandleChangeResult = (num: number) => {
    if (iframeRef.value?.contentWindow) {
        const datas = {
            type: "handleChangeResult",
            data: num
        };
        iframeRef.value.contentWindow.postMessage(datas, "*");
    }
}
